package ece.pdx.finalproject.roleplay;

import android.support.v4.app.FragmentActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.fourmob.datetimepicker.date.DatePickerDialog;
import com.fourmob.datetimepicker.date.DatePickerDialog.OnDateSetListener;


public class DbAddActivity extends FragmentActivity implements OnClickListener {

	// GUI components
	private EditText todoText;		// Text field
	private Button addNewButton;	// Add new button
	private Button backButton;		// Back button
 	private Spinner spinner1;
	String date;
	private boolean isEdit;
	private int idToDo;
	
	// DAO
	private DataBaseHandler dbHandler;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_todo);
	
		// Create DAO object
		dbHandler = new DataBaseHandler(this);
		isEdit = getIntent().getExtras().getBoolean("Edit");
		idToDo=getIntent().getExtras().getInt("Id");
		todoText 		= (EditText)findViewById(R.id.newTodoText);
		addNewButton 	= (Button)findViewById(R.id.addNewTodoButton);
		backButton		= (Button)findViewById(R.id.menuGoBackButton);
		if(isEdit)
			addNewButton.setText("Edit");
		spinner1 = (Spinner) findViewById(R.id.spinner1);
		spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
		addNewButton.setOnClickListener(this);
		backButton.setOnClickListener(this);
		final DatePickerDialog datePickerDialog = new DatePickerDialog();
		datePickerDialog.setOnDateSetListener(new OnDateSetListener() {

			@Override
			public void onDateSet(DatePickerDialog datePickerDialog, int year, int month, int day) {
			   date=new String(String.valueOf(year)+"-"+String.valueOf(month+1)+"-"+String.valueOf(day));
			}
		});

		findViewById(R.id.button1).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				datePickerDialog.show(getSupportFragmentManager(), "datepicker");
			}
		});
	}

	@Override
	public void onClick(View v) {
		// If add button was clicked
		if (addNewButton.isPressed()) {
			// Get entered text
			if(todoText.getText().toString().equalsIgnoreCase(""))
			{
				Toast.makeText(this, "Task Field Empty",Toast.LENGTH_SHORT).show();
			}
			else
			{
			String todoTextValue = todoText.getText().toString();
			
			todoText.setText("");
			ToDoList list= new ToDoList();
			list.setText(todoTextValue);
			list.setRole(String.valueOf(spinner1.getSelectedItem()));
			list.setDate(date);
			if(isEdit==true)
			{

				int edited=dbHandler.updateToDo(list,idToDo);
				Toast.makeText(getApplicationContext(), "New TODO edited! "+String.valueOf(edited), Toast.LENGTH_LONG).show();

			}
			else
			{
			// Add text to the database
			dbHandler.createTodo(list);
			
			// Display success information
			Toast.makeText(getApplicationContext(), "New TODO added!", Toast.LENGTH_LONG).show();
			}}
		} else if (backButton.isPressed()) {
			// When back button is pressed
			// Create an intent
			Intent intent = new Intent(this, MainActivity.class);
			// Start activity
			startActivity(intent);
			// Finish this activity
			this.finish();
			
			// Close the database
			dbHandler.close();
		}
		
	}
	
}
